let mock = false;
setTimeout(() => {
    // mock = !window.AutoJs;
}, 1000);
const mockData = {
    toast: null,
};

/**
 * 发起请求
 * @param {*} method
 * @param {*} params
 */
const aj = (method, params) => {
    params = params || {};
    // console.log(`请求 [${method}]: ${JSON.stringify(params)}`);
    if (mock) {
        let data = mockData[method];
        console.log('Mock request [' + method + "] return:\t\r" + JSON.stringify(data));
        return new Promise(res => {
            res(data);
        });
        // return data;
    }
    return new Promise((res, rej) => {
        let interval = null;
        interval = setInterval(() => {
            if (!window.AutoJs) {
                console.log('等待autojs注入...');
                return;
            }
            clearInterval(interval);
            window.AutoJs.invoke(method, params, 
                (resp) => {
                    // console.log('接收到:' + JSON.stringify(data));
                    if (resp.success) {
                        res(resp.data);
                    } else {
                        console.warn(`请求失败，method=${method}, 返回： ${JSON.stringify(resp)}`);
                        // Notify({ type: 'danger', message: res.message });
                        Toast.fail(resp.message || `请求失败：\nmethod【${method}】`);
                    }
                }
            );
        }, 500);
    });
};